/**
 * Copyright &copy; 2016-2022 liuhangjun All rights reserved.
 */
package com.channelsharing.hongqu.supplier.api.controller.seckillgoods;

import com.channelsharing.common.entity.Paging;
import com.channelsharing.common.exception.SystemInnerBusinessException;
import com.channelsharing.common.utils.BeanUtil;
import com.channelsharing.hongqu.supplier.api.controller.BaseController;
import com.channelsharing.hongqu.supplier.api.entity.GoodsInfo;
import com.channelsharing.hongqu.supplier.api.entity.ProductInfo;
import com.channelsharing.hongqu.supplier.api.entity.SecKillGoods;
import com.channelsharing.hongqu.supplier.api.service.GoodsInfoService;
import com.channelsharing.hongqu.supplier.api.service.ProductInfoService;
import com.channelsharing.hongqu.supplier.api.service.SecKillGoodsService;
import com.sun.tools.internal.xjc.model.nav.EagerNClass;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 秒杀商品信息接口Controller
 * @author liuhangjun
 * @version 2018-06-06
 */
@Api(tags = "秒杀商品信息操作接口")
@Validated
@RestController
@RequestMapping("/v1/seckillgoods")
public class SecKillGoodsController extends BaseController {

	@Resource
	private SecKillGoodsService secKillGoodsService;
	@Resource
	private ProductInfoService productInfoService;

    @ApiOperation(value = "获取单条秒杀商品信息")
	@GetMapping("/{id}")
	public SecKillGoods findOne(@PathVariable Long id){
	    SecKillGoods secKillGoods = secKillGoodsService.findOne(id);
	    if (secKillGoods == null)
			secKillGoods =  new SecKillGoods();
		return secKillGoods;
	}

	@ApiOperation(value = "获取商品信息列表-简单搜索接口")
	@GetMapping("/list")
	public Paging<SecKillGoods> findPaging(
			@RequestParam(defaultValue = "0") Integer offset,
			@RequestParam(defaultValue = "10") Integer limit){

		SecKillGoods entity = new SecKillGoods();
		entity.setOffset(offset);
		entity.setLimit(limit);
		return secKillGoodsService.findPaging(entity);

	}

	@ApiOperation(value = "提交一条秒杀商品信息")
	@PostMapping
	public SecKillGoods add(@RequestBody @Validated SecKillGoodsAddRequestEntity secKillGoodsAddRequestEntity){
		ProductInfo productInfo = productInfoService.findOne(secKillGoodsAddRequestEntity.getProductId());
		if(productInfo == null || productInfo.getStoreNumber() < secKillGoodsAddRequestEntity.getRestAmount()){
			throw new SystemInnerBusinessException("商品不存在或库存不足");
		}

		SecKillGoods entity = new SecKillGoods();
		entity.setProductId(secKillGoodsAddRequestEntity.getProductId());
		SecKillGoods secKillGoods = secKillGoodsService.findOne(entity);
		if(null != secKillGoods && secKillGoods.getEndTime().getTime() > System.currentTimeMillis()){
			throw new SystemInnerBusinessException("当前产品存在尚未结束的秒杀活动");
		}
		if(secKillGoods.getSecKillPrice().compareTo(productInfo.getUnitPrice())<0){
			throw new SystemInnerBusinessException("秒杀价不能低于成本价");
		}

		BeanUtils.copyProperties(secKillGoodsAddRequestEntity,entity);
		entity.setSupplierId(super.currentSupplierUser().getSupplierId());

		SecKillGoods result = secKillGoodsService.addWithResult(entity);

		return result;
	}

	/**
	 * 修改秒杀商品信息
	 * @param secKillGoodsModifyRequestEntity
	 */
	@ApiOperation(value = "修改一条商品信息")
	@PatchMapping
	public void modify(@RequestBody @Validated SecKillGoodsModifyRequestEntity secKillGoodsModifyRequestEntity){
		SecKillGoods secKillGoods = secKillGoodsService.findOne(secKillGoodsModifyRequestEntity.getId());
		if(secKillGoods == null){
			throw new SystemInnerBusinessException("秒杀产品不存在");
		}
		if(System.currentTimeMillis() > secKillGoods.getStartTime().getTime()){
			throw new SystemInnerBusinessException("秒杀产品已经开始或结束无法修改");
		}
		if(secKillGoodsModifyRequestEntity.getRestAmount() != null){
			ProductInfo productInfo = productInfoService.findOne(secKillGoods.getProductId());
			if(secKillGoodsModifyRequestEntity.getRestAmount() > productInfo.getStoreNumber()){
				throw new SystemInnerBusinessException("参与秒杀的产品库存不足");
			}
		}
		SecKillGoods entity = new SecKillGoods();
		BeanUtils.copyProperties(secKillGoodsModifyRequestEntity, entity);

		secKillGoodsService.modify(entity);
	}


	@ApiOperation(value = "删除一条秒杀商品信息")
	@DeleteMapping("/{id}")
	public void delete(@PathVariable Long id){
	    SecKillGoods entity = new SecKillGoods();
	    entity.setId(id);
		SecKillGoods secKillGoods = secKillGoodsService.findOne(id);
		// 已经开始的产品不可以删除
		if (secKillGoods !=null && secKillGoods.getStartTime().getTime() < System.currentTimeMillis()){
		    throw new SystemInnerBusinessException("秒杀已经开始或已结束");
		}

		secKillGoodsService.delete(entity);
	}


}
